// pages/mine/mine.js
const getApps = getApp().globalData
const imgUrl = getApps.imgUrl
const util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  /**
   * 页面的初始数据
   */
  data: {
    app_id: getApp().globalData.app_id == 'aQ%3D%3D' ? true : false,
    imgUrls: getApps.imgUrls,
    couponNum: 0, // 优惠券的个数
    data: [],
    background: `background:url( ${imgUrl}jsynewshopMineBg.png) no-repeat;background-size: 100%`,
    imgUrl,
    right_r: imgUrl + "jsynewshopChevron.svg",
    userNickName: wx.canIUse('open-data.type.userNickName'),
    dingDanlist: [{
      status: 0,
      image: imgUrl + "jsy-newshop待付款.svg",
      name: "待付款"
    },
    {
      status: 1,
      image: getApps.imgUrls + "wx3dg5daishouhuo.svg",
      name: "待发货"
    },
    {
      status: 3,
      image: imgUrl + "jsynewsh待收货.svg",
      name: "待收货"
    },
    // {
    //   status: 2,
    //   image: imgUrl + "jsy-newshop退换.svg",
    //   name: "退款/售后"
    // },
    {
      status: 99,
      image: imgUrl + "jsy-newshop订单.svg",
      name: "我的订单",
      color: " color:rgb(208,178,133)"
    },
    ]
  },

  jumpVip(e) {
    let stasus = e.currentTarget.dataset.cardstatus
    let index = e.currentTarget.dataset.index
    if (this.data.data.card_id) { //判断是否有卡
      if (stasus != 1) {
        if (stasus == 2) { // == 2 是有卡
          wx.navigateTo({
            url: `../Vip/Vip?fromMine=0&card_id=${this.data.data.card_id}&stasus=true&index=${index}`,
          })
        } else if (stasus == 3) {
          wx.navigateTo({
            url: `../Vip/Vip?fromMine=0&card_id=${this.data.data.card_id}&stasus=false&index=${index}`,
          })
        }
      }
    }
  },
  onShareAppMessage() {
    var that = this
    return {
      title: getApp().globalData.app_name,
      path: 'pages/index/index?scene=normal'+ `&member_id=${getApp().globalData.member_id}&type=3`,
      success: () => {
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: () => {
        wx.showToast({
          title: '分享失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onPullDownRefresh() { },
  getUserList() { //获取mine  页面的数据
    util.API('spwx/v1.shop/user_list').then(res => {
      let {
        data,
        code,
        msg
      } = res
      if (code == 200) {
        console.log(res)
        this.setData({
          data
        })
        this.getCoupon() //获取卡的信息
      }
    })
  },
  onLoad: function (options) {
    getApp().getPagesurl()
    // this.getUserList()
  },
  onShow() {
    this.getUserList()
  },
  getCoupon() { //   获取优惠券有关信息
    util.API('spwx/v1.shop/coupon_list', {
      status: '0'
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code == 200) {
        this.setData({
          couponNum: data.total
        })
      }
      console.log(data, 11111)
    })
  },
  phoneTag() {
    wx.makePhoneCall({
      phoneNumber: '0451-86632366',
    })
  },
  jumpOrder: function (e) {
    if (e.currentTarget.dataset.status != 2) {
      wx.navigateTo({
        url: '/pages/dingDanList/dingDanList?status=' + e.currentTarget.dataset.status
      })
    } else {
      wx.navigateTo({
        url: '/pages/tuiKuanDingDan/tuiKuanDingDan?status=' + e.currentTarget.dataset.status
      })
    }
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showToast({
        title: '未授权',
        icon: 'none'
      })
    } else {
      wx.request({
        url: getApps.URL + 'wxcheck/wxcheck/decryptData',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          appid: getApps.wx_appid,
          sessionKey: getApps.loginData.session_key
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          res.data = res.data.split('}}')[0] + "}}"
          res.data = JSON.parse(res.data)
          util.API('wxcheck/wxcheck/savephone', {
            openid: getApps.loginData.openid,
            phone: res.data.phoneNumber
          }).then(res => {

            if (res.code == 200) {
              wx.showToast({
                title: '绑定成功',
              })
            }
          })
        }
      })

    }
  }
}))